package br.com.educa.modelo.publico;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.serializable.RooSerializable;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table
@Configurable
@RooJson
@RooSerializable
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Controle implements Serializable{

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Controle().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countControles() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Controle o", Long.class).getSingleResult();
    }

	public static List<Controle> findAllControles() {
        return entityManager().createQuery("SELECT o FROM Controle o", Controle.class).getResultList();
    }

	public static Controle findControle(Long id) {
        if (id == null) return null;
        return entityManager().find(Controle.class, id);
    }

	public static List<Controle> findControleEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Controle o", Controle.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Controle attached = Controle.findControle(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Controle merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Controle merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;
	
	@NotNull
	private String unidade;
	
	@NotNull
	private String endereco;
	
	@ManyToOne
	@JoinColumn(name = "bairro", referencedColumnName = "id")
	private Bairro bairro;
	
	@ManyToOne
	@JoinColumn(name = "cidade", referencedColumnName = "id")
	private Cidade cidade;
	
	private String cep;
	
	private String telefone;
	
	private String fax;

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

	public static Controle fromJsonToControle(String json) {
        return new JSONDeserializer<Controle>().use(null, Controle.class).deserialize(json);
    }

	public static String toJsonArray(Collection<Controle> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

	public static Collection<Controle> fromJsonArrayToControles(String json) {
        return new JSONDeserializer<List<Controle>>().use(null, ArrayList.class).use("values", Controle.class).deserialize(json);
    }

	private static final long serialVersionUID = 1L;

	public String getUnidade() {
        return this.unidade;
    }

	public void setUnidade(String unidade) {
        this.unidade = unidade;
    }

	public String getEndereco() {
        return this.endereco;
    }

	public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

	public Bairro getBairro() {
        return this.bairro;
    }

	public void setBairro(Bairro bairro) {
        this.bairro = bairro;
    }

	public Cidade getCidade() {
        return this.cidade;
    }

	public void setCidade(Cidade cidade) {
        this.cidade = cidade;
    }

	public String getCep() {
        return this.cep;
    }

	public void setCep(String cep) {
        this.cep = cep;
    }

	public String getTelefone() {
        return this.telefone;
    }

	public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

	public String getFax() {
        return this.fax;
    }

	public void setFax(String fax) {
        this.fax = fax;
    }
}
